package com.ybkj.daijia.api.apirest;

import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.api.driver.transport.ResultTransport;
import com.ybkj.daijia.api.driver.v4.DriverAccountController;
import com.ybkj.daijia.api.util.ErrorConstantV4;
import com.ybkj.daijia.server.event.model.ActivityCheckEvent;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.reactor.ReactorVo;
import com.ybkj.daijia.server.sales.Activity.ActivityEvent;
import com.ybkj.daijia.server.service.ActivityService;
import com.ybkj.daijia.server.service.FirstOrderPassengerService;
import com.ybkj.daijia.server.service.MemberService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller("ApiActivityProcessController")
@RequestMapping(value = "api/activity/")
public class ActivityProcessController {

    private static final Logger logger = LoggerFactory.getLogger(DriverAccountController.class);

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private MemberService memberService;

    @Autowired
    private ApplicationEventMulticaster applicationEventMulticaster;

    @Autowired
    private FirstOrderPassengerService firstOrderPassengerService;

    @Autowired
    private ActivityService activityService;

    /**
     * 新客户活动
     *
     * @return
     */
    @RequestMapping(value = "newPassengerActivity", method = RequestMethod.POST)
    public void newPassengerActivity(Long id) {

        Passenger passenger = memberService.findOnePassenger(id);
        if (null != passenger) {
            ReactorVo rVo = new ReactorVo();
            rVo.setPassenger(passenger);
            rVo.setActivityEvent(ActivityEvent.newRegister);
            ActivityCheckEvent activityCheckEvent = new ActivityCheckEvent(rVo);
            applicationEventMulticaster.multicastEvent(activityCheckEvent);
        }

    }

    /**
     * 首单免费活动
     *
     * @param passengerId
     * @param orderType        订单类型
     * @param orderFromSource
     */

    @RequestMapping(value = "freeFirstOrderActivity", method = RequestMethod.POST)
    public @ResponseBody
    ResultTransport freeFirstOrderActivity(Long passengerId, String orderType,
        String orderFromSource) {

        logger.debug("recv passengerId{},type:{},orderFromSource:{}", passengerId, orderType,
            orderFromSource);

        if (StringUtils.isBlank(String.valueOf(passengerId)) || StringUtils.isBlank(orderType)
            || StringUtils.isBlank(orderFromSource)) {

            return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
        }
        Passenger passenger = memberService.findOnePassenger(passengerId);
        if (null == passenger) {
            return ResultTransport.getErrorBy(ErrorConstantV4.PASSENGER_NOT_EXISTS);
        }

        try {

            Boolean success = activityService
                .findFreeFirstOrder(passengerId, orderType, orderFromSource);
            ResultTransport transport = new ResultTransport();
            if (success) {
                transport = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
            }
            return transport;

        } catch (Exception e) {
            logger.error("api v4 driver login error: {}", e);
            return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
        }
    }

}
